﻿using System;
using System.Linq;
using System.Text;
using System.Data.Common;
using System.Linq.Expressions;
using System.Collections.Generic;
using System.Threading.Tasks;
using YiSha.Util;
using YiSha.Util.Extension;
using YiSha.Util.Model;
using YiSha.Data;
using YiSha.Data.Repository;
using YiSha.Entity.OrganizationManage;
using YiSha.Model.Param.OrganizationManage;

namespace YiSha.Service.OrganizationManage
{
    /// <summary>
    /// 创 建：admin
    /// 日 期：2020-05-09 22:50
    /// 描 述：产品信息服务类
    /// </summary>
    public class MotoService : RepositoryFactory
    {
        #region 获取数据
        public async Task<List<MotoEntity>> GetList(MotoListParam param)
        {
            var expression = ListFilter(param);
            var list = await this.BaseRepository().FindList(expression);
            return list.ToList();
            
        }

        public async Task<List<MotoEntity>> GetPageList(MotoListParam param, Pagination pagination)
        {

            var expression = ListFilter(param);
            var list= await this.BaseRepository().FindList(expression, pagination);
            return list.ToList();
         
        }

        public async Task<MotoEntity> GetEntity(long id)
        {
            return await this.BaseRepository().FindEntity<MotoEntity>(id);
        }
        public async Task<int> GetMaxSort()
        {
            object result = await this.BaseRepository().FindObject("SELECT MAX(moto_sort) FROM sys_moto");
            int sort = result.ParseToInt();
            sort++;
            return sort;
        }
        #endregion

        #region 提交数据
        public async Task SaveForm(MotoEntity entity)
        {
            if (entity.Id.IsNullOrZero())
            {
                await entity.Create();
                await this.BaseRepository().Insert(entity);
            }
            else
            {
                await entity.Modify();
                await this.BaseRepository().Update(entity);
            }
        }

        public async Task DeleteForm(string ids)
        {
            long[] idArr = TextHelper.SplitToArray<long>(ids, ',');
            await this.BaseRepository().Delete<MotoEntity>(idArr);
        }
        #endregion

        #region 私有方法
        //private Expression<Func<MotoEntity, bool>> ListFilter(MotoListParam param)
        //{
        //    var expression = LinqExtensions.True<MotoEntity>();
        //    if (param != null)
        //    {
        //    }
        //    return expression;
        //}
        private Expression<Func<MotoEntity, bool>> ListFilter(MotoListParam param)
        {
            var expression = LinqExtensions.True<MotoEntity>();
            if (param != null)
            {
                if (!string.IsNullOrEmpty(param.MotoTitle))
                {
                    expression = expression.And(t => t.MotoTitle.Contains(param.MotoTitle));
                }
                if (!string.IsNullOrEmpty(param.MotoTitle))
                {
                    expression = expression.And(t => t.MotoTitle.Contains(param.MotoTitle));
                }
                if (!string.IsNullOrEmpty(param.MotoTitle))
                {
                    expression = expression.And(t => t.MotoTitle.Contains(param.MotoTitle));
                }

            }
            return expression;
            #endregion
        }
    }
}
